<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of 4399
 *
 * @author dragon.hu add @2016.2.2
 */
require_once 'BaseChannel.php';

//4399平台服务器端接入
class M4399 implements BaseChannel {

  //put your code here
    function __construct() {
		
    }
	
    function android($data) 
    {
    	$cpId = 110390; 
		$apiKey = '007aaac31d53cb220ca96025f9a934c6';  //守护者们
    	echo $this->payCallBackMethod($cpId, $apiKey, ANDROID);
    }

    function ios($data) 
    {
    	$cpId = 110390; 
		$apiKey = '007aaac31d53cb220ca96025f9a934c6';  //守护者们
        echo $this->payCallBackMethod($cpId, $apiKey, IOS);
    }
    
	/**
	 * 支付回调通知。
	 * 注：
	 * 1.sdk server通过http post请求cp server的接口，cp server都要有一个响应，响应内容是payCallback函数的返回值
	 * $platform:平台名字
	 */
	public function payCallBackMethod($cpId, $apiKey, $platform){
		
		//$request = file_get_contents("php://input");
		//echo " request:$request \r\n";
		//var_dump($_REQUEST);
		$request = $_REQUEST;
		$orderLog = new ChargeOrder();
		$orderLog->log("m4399", $_REQUEST);
		try{
			//	加密签名，签名计算为：$sign = md5($orderid . $uid . $money . $gamemoney . $serverid . $secrect . $mark . $roleid.$time); 
            //  当参数$serverid,$mark ,$roleid为空时，不参与签名计算。详见[签名说明]。
			$signSource = $_REQUEST['orderid'].$_REQUEST['uid'].$_REQUEST['money'].$_REQUEST['gamemoney'].$_REQUEST['serverid'].$apiKey.$_REQUEST['mark'].$_REQUEST['roleid'].$_REQUEST['time'];
			
			$sign = md5 ( $signSource );
			
			if ($sign == $_REQUEST['sign']) 
			// if(1==1) //for test
			{
				$status = STATUS_SUCCESS;  //sign验证成功就表示成功

				if (false == $orderLog->recordOrder ( $status, $_REQUEST['money'], $_REQUEST['mark'], "m4399", $platform, $_REQUEST['orderid'] )) {
					$orderLog->log ( "m4399", "money:" . $_REQUEST['money'] . " mark:" . $_REQUEST['mark'] . " platform:" . $platform . " failure" );
					//返回成功结果
					$Result ["status"] = 1;  //异常
					$Result ["code"] = "other_error";
					$Result ["money"] = $_REQUEST ['money'];
					$Result ["gamemoney"] = $_REQUEST ['gamemoney'];
					$Result ["msg"] = urlencode ( "充值失败" );
					$Res = json_encode ( $Result );
					return urldecode ( $Res );
				}
				
				$orderLog->log ( "m4399", "money:" . $_REQUEST['money'] . " mark:" . $_REQUEST['mark'] . " platform:" . $platform . " success" );
				//返回成功结果
				$Result ["status"] = 2;  //成功
				$Result ["code"] = "";
				$Result ["money"] = $_REQUEST ['money'];
				$Result ["gamemoney"] = $_REQUEST ['gamemoney'];
				$Result ["msg"] = urlencode ( "充值成功" );
				$Res = json_encode ( $Result );
				return urldecode ( $Res );
			} else {
				// echo " failure2 \r\n";
				$orderLog->log ( "m4399", " money:" . $_REQUEST['money'] . " sign failure \r\n" );
				//返回成功结果
				$Result ["status"] = 1;  //异常
				$Result ["code"] = "sign_error";
				$Result ["money"] = $_REQUEST ['money'];
				$Result ["gamemoney"] = $_REQUEST ['gamemoney'];
				$Result ["msg"] = urlencode ( "充值失败" );
				$Res = json_encode ( $Result );
				return urldecode ( $Res );
			}		
			
		}catch(Exception $e){
			$orderLog->log("m4399",  " unknow error \r\n");
			throw new exception($e->getMessage());
			//echo "unknow error";
		}
		//返回成功结果
		$Result ["status"] = 3;  //失败
		$Result ["code"] = "";
		$Result ["money"] = $_REQUEST ['money'];
		$Result ["gamemoney"] = $_REQUEST ['gamemoney'];
		$Result ["msg"] = urlencode ( "充值失败" );
		$Res = json_encode ( $Result );
		return urldecode ( $Res );
	}
    
}
?>